<?php
            
use app\model\Record; 
use app\model\Theme; 
use app\model\Worker; 
use app\model\Userdata; 
use app\model\Relation; 
use app\model\Otherform; 


/**
 * Desc:抽奖首页
 * Time: 2021/3/22 5:49 下午
 */
class Index_Page extends PluginMobilePage {

    function main()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if ($fromtype=='mobile') {
            $num = isset($_P['num'])?$_P['num']:8;
            $return_client = get_defined_vars();
        return $this->view("lottery/index/index", $return_client);
            return; //退出程序执行
        } elseif ($fromtype=='wxapp') {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            $workerid = isset($_P['workerid'])?$_P['workerid']:'';
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);

            if (!$themeid || !$theme['id']) {
                return_json(1,'活动入口不正确');
            } else {
                //基本检测
                $fans = $this->checkuser();
                //判断用户是否存在订单
                $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
                $res = LotteryModel::indexMethod($theme,$_C,$fans['uid'],$fromuid,$themyrecord);

                $format_res = $res;
                return_json(0,'',$format_res);
            }
        }
    }

    function post()
    {
        global $_P,$_G,$_S,$_C;
        $config = $_C;
        $fromtype = $_P['from'];
        if (!$_G['ispost']) {
            return_json(1,'非法请求');
        } else {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            $op = isset($_P['op'])?$_P['op']:'display';
            if (!$themeid || !$theme['id']) {
                return_json(1,'活动入口不正确');
            }
            else {
                $fans = $this->checkuser();
                $fromuid = $_P['fromuid']?$_P['fromuid']:$fans['uid'];


                //底部按钮
                // 16 返回首页
                // 61 助力TA
                // 62 已帮TA助力
                // 63 查看我的抽奖
                // 64 我也要参与
                // 999 立即邀请
                if (isset($config['yx_lt_mode']) && $config['yx_lt_mode']==1 && $op=='display'){
                    $bottombut = [
                        'ft'=>['size'=>$config['yx_lt_ft_size'],'bold'=>$config['yx_lt_ft_bold']]
                    ];
                    $bottombut['list']['left'] =['txt'=>$config['yx_lt_home_txt'],'url'=>16,'c'=>$config['yx_lt_home_txt_c'],'c1'=>$config['yx_lt_home_txt_c1'],'c2'=>$config['yx_lt_home_txt_c2']];
//                无上级 自身存在抽奖
                    if (!$fromuid || ($fromuid==$fans['uid'])){
                        $bottombut['list']['center'] =['txt'=>$config['yx_lt_invite_txt'],'url'=>999,'c'=>$config['yx_lt_invite_txt_c'],'c1'=>$config['yx_lt_invite_txt_c1'],'c2'=>$config['yx_lt_invite_txt_c2']];
                        $bottombut['list']['right'] =['txt'=>'','url'=>0,'c1'=>'','c2'=>''];
                    }
                    if ($fromuid || $fromuid!=$fans['uid']){
                        $from_fans = LotteryModel::getUserInfoById($fromuid,$themeid);
                        $has_friend_flag = LotteryModel::getFriendViewChanceFlag($themeid,$from_fans['uid'],$fans['uid'],'friend_help');
                        $remain_num = $fans['jihui'];
                        $user_chance_flag_num = LotteryModel::getUserChanceByUidAndThemeId($fans['uid'],$themeid);

                        if ($has_friend_flag){
                            $bottombut['list']['center'] =['txt'=>$config['yx_lt_powered_txt'],'url'=>62,'c'=>$config['yx_lt_powered_txt_c'],'c1'=>$config['yx_lt_powered_txt_c1'],'c2'=>$config['yx_lt_powered_txt_c2']];
                            if ($remain_num<0 && $user_chance_flag_num<0){
                                //存在上级-存在助力-自身无抽奖
                                $bottombut['list']['right'] =['txt'=>$config['yx_lt_join_txt'],'url'=>64,'c'=>$config['yx_lt_join_txt_c'],'c1'=>$config['yx_lt_join_txt_c1'],'c2'=>$config['yx_lt_join_txt_c2']];
                            }elseif ($remain_num>0 || $user_chance_flag_num>0){
                                //存在上级-存在助力-自身有抽奖
                                $bottombut['list']['right'] =['txt'=>$config['yx_lt_my_txt'],'url'=>63,'c'=>$config['yx_lt_my_txt_c'],'c1'=>$config['yx_lt_my_txt_c1'],'c2'=>$config['yx_lt_my_txt_c2']];
                            }
                        }
                        if (!$has_friend_flag){
                            $bottombut['list']['center'] =['txt'=>$config['yx_lt_power_txt'],'url'=>61,'c'=>$config['yx_lt_power_txt_c'],'c1'=>$config['yx_lt_power_txt_c1'],'c2'=>$config['yx_lt_power_txt_c2']];
                            if ($remain_num<0 && $user_chance_flag_num<0){
                                //存在上级-不存在助力-自身无抽奖
                                $bottombut['list']['right'] =['txt'=>$config['yx_lt_join_txt'],'url'=>64,'c'=>$config['yx_lt_join_txt_c'],'c1'=>$config['yx_lt_join_txt_c1'],'c2'=>$config['yx_lt_join_txt_c2']];
                            }elseif ($remain_num>0 || $user_chance_flag_num>0){
                                //存在上级-存不在助力-自身有抽奖
                                $bottombut['list']['right'] =['txt'=>$config['yx_lt_my_txt'],'url'=>63,'c'=>$config['yx_lt_my_txt_c'],'c1'=>$config['yx_lt_my_txt_c1'],'c2'=>$config['yx_lt_my_txt_c2']];
                            }
                        }
                    }
                }elseif (isset($config['yx_lt_mode']) && $config['yx_lt_mode']==1 && $op=='my'){
                    $bottombut = [
                        'ft'=>['size'=>$config['yx_lt_ft_size'],'bold'=>$config['yx_lt_ft_bold']]
                    ];
                    $bottombut['list']['left'] =['txt'=>$config['yx_lt_home_txt'],'url'=>16,'c'=>$config['yx_lt_home_txt_c'],'c1'=>$config['yx_lt_home_txt_c1'],'c2'=>$config['yx_lt_home_txt_c2']];
                    $bottombut['list']['center'] =['txt'=>$config['yx_lt_invite_txt'],'url'=>999,'c'=>$config['yx_lt_invite_txt_c'],'c1'=>$config['yx_lt_invite_txt_c1'],'c2'=>$config['yx_lt_invite_txt_c2']];
                }

                $from_fans = [];
                //判断新模式
                $friend_visit = false;
                $can_help_friend = false;
                $lt_diy_prize_imgs = $config['lt_diy_prize_imgs'];
                $yx_lt_help_target = $config['yx_lt_help_target'];
                if (isset($config['yx_lt_mode']) && $config['yx_lt_mode']==1 && $op=='display'){
                    //判断当前的用户与上级
                    if (isset($fromuid) && $fromuid && $fromuid!=$fans['uid']){
                        $friend_visit = true;
                        //判断上级是否参与抽奖
                        $from_fans = LotteryModel::getUserInfoById($fromuid,$themeid);
                        $from_fans_lt_flag = LotteryModel::getUserFlags($themeid,$fromuid);
                        if ($from_fans['jihui']>0 || $from_fans_lt_flag>0){
                            //上级参与抽奖
                            $has_friend_flag = LotteryModel::getFriendViewChanceFlag($themeid,$from_fans['uid'],$fans['uid'],'friend_help');
                            $is_new_fans = LotteryModel::checkNewUser($fans,$config);
                            //判断from粉丝机会是否达到上限
                            //判断是否是新用户
                            if ($from_fans && $is_new_fans && !$has_friend_flag){
                                $themyrecord = Record::getLastGroupByUid($from_fans['uid'],$themeid);
                                //判断上一级是否支付
                                if ($config['yx_lt_help_user_type'] && $themyrecord){
                                    $can_help_friend = true;
                                }
                                if(!$config['yx_lt_help_user_type']){
                                    $can_help_friend = true;
                                }
                                $can_help_friend = true;
                            }
                        }
                        $fans = $from_fans;
                    }
                }
                //兼容老模式
                if ($config['yx_lt_friend_help']>0 && $config['yx_lt_friend_help_limit']>0){
                    //判断当前的用户与上级
                    if (isset($fromuid) && $fromuid && $fromuid!=$fans['uid']){
                        $friend_visit = true;
                        //判断上级是否参与抽奖
                        $from_fans = LotteryModel::getUserInfoById($fromuid,$themeid);
                        $from_fans_lt_flag = LotteryModel::getUserFlags($themeid,$fromuid);
                        //上级参与抽奖
                        $has_friend_flag = LotteryModel::getFriendViewChanceFlag($themeid,$from_fans['uid'],$fans['uid'],'friend_help');
                        //判断from粉丝机会是否达到上限
                        if ($from_fans && !$has_friend_flag){
                            $themyrecord = Record::getLastGroupByUid($from_fans['uid'],$themeid);
                            //判断上一级是否支付
                            if ($config['yx_lt_help_user_type'] && $themyrecord){
                                $can_help_friend = true;
                            }
                            if(!$config['yx_lt_help_user_type']){
                                $can_help_friend = true;
                            }
                            $can_help_friend = true;
                        }else{
                            $from_fans = [];
                        }
                    }
                }



                $res = LotteryModel::indexMethod($theme,$_C,$fans['uid'],$fromuid,$themyrecord);
                $error_txt = isset($res['error_txt'])?$res['error_txt']:'';
                $top_img = isset($res['top_img'])?$res['top_img']:'';
                $lt_btn_txt = isset($res['lt_btn_txt'])?$res['lt_btn_txt']:'抽奖';
                $remain_num = isset($res['remain_num'])?$res['remain_num']:0;
                $tips = isset($res['tips'])?$res['tips']:'';
                $can_lt = isset($res['can_lt'])?$res['can_lt']:false;
                $show_no_chance_num = $res['show_no_chance_num'];

                $prize_info = isset($res['prize_info'])?$res['prize_info']:[];
                $record_list = isset($res['record_list'])?$res['record_list']:[];
                $method_list = isset($res['method_list'])?$res['method_list']:[];
                $lose_lottery_info = isset($res['lose_lottery_info'])?$res['lose_lottery_info']:[];
                $rules = isset($res['rules'])?$res['rules']:[];
                $has_lucky_star = isset($res['has_lucky_star'])?$res['has_lucky_star']:false;
                $lucky_star_info = isset($res['lucky_star_info'])?$res['lucky_star_info']:[];
                $open_red_package = isset($res['open_red_package'])?$res['open_red_package']:false;
                $my_prize = isset($res['my_prize'])?$res['my_prize']:[];
                $show_tip = isset($res['show_tip'])?$res['show_tip']:'';
                $show_my_prize = isset($res['show_my_prize'])?$res['show_my_prize']:false;
                $help_list = [];//到访
                $friend_help_arr = [];//助力
                $friend_order_arr = [];//好友下单

                //判断用户是否存在订单
                $used_lt = 0; //不限制 抽奖条件 1已下单可抽奖  2未下单不可抽奖
//                $themyrecord = Record::getRecordByUid($fans['uid'],$themeid);
                $themyrecord = db_get(t_record,['uid'=>$fans['uid'],'themeid'=>$themeid,'status >'=>0]);
                if ($_C['yx_lt_con']==1){
                    if ($themyrecord){
                        $used_lt = 1;
                    }else{
                        $used_lt = 2;
                        $tips ='抢购后即可参与抽奖';
                        $lt_btn_txt = '请先抢购';
                    }
                }
                //补余款可以抽
                if ($_C['yx_lt_con']==2){
                    $yukuan_order = Record::getRecordYukuanByUid($_C,$fans['uid'],$themeid);
                    if ($yukuan_order){
                        $used_lt = 1;
                    }else{
                        $used_lt = 2;
                        $tips ='补款后即可参与抽奖';
                        $lt_btn_txt = '请先补款';
                    }
                }
                //收集客资的条件 1 抽奖前  2抽奖后 0已填写 不需要
                $yx_lt_receive_con = 0;
                if (!$_C['yx_lt_receive_con'] || $_C['yx_lt_receive_con'] ==1){
                    if (!$fans['username'] || !$fans['mobile']){
                        if (!$_C['yx_lt_receive_con']){
                            $yx_lt_receive_con = 1;
                        }
                        $used_flag = LotteryModel::getUserFlags($themeid,$fans['uid']);
                        if ($_C['yx_lt_receive_con']==1){
                            if ($used_flag){
                                $yx_lt_receive_con = 2;
                            }
                        }
                    }
                }

                if ($fromtype=='wxapp' && (isset($config['yx_lt_mode']) && $config['yx_lt_mode']==1)){
                    //到访
                    $friend_help_list = LotteryModel::getFriendHelpInfo($themeid,$_C,$fans['uid'],0,12,'friend_view');
                    //助力
                    $help_friend_list = LotteryModel::getFriendHelpInfo($themeid,$_C,$fans['uid'],0,12,'friend_help');
                    //好友下单
                    $friend_order_list = LotteryModel::getFriendHelpInfo($themeid,$_C,$fans['uid'],0,12,'friend_order');
                }else{
                    $friend_help_list = LotteryModel::getFriendHelpInfo($themeid,$_C,$fans['uid']);
                    //助力
                    $help_friend_list = LotteryModel::getFriendHelpInfo($themeid,$_C,$fans['uid'],0,12,'friend_help');
                    //好友下单
                    $friend_order_list = LotteryModel::getFriendHelpInfo($themeid,$_C,$fans['uid'],0,12,'friend_order');
                }



                //计算总数
                $set_show_num = $set_help_num = 6;
                //好友到访
                $yx_lt_friend_view = (int)$_C['yx_lt_friend_view'];
                $yx_lt_friend_view_limit = (int)$_C['yx_lt_friend_view_limit'];
                if ($yx_lt_friend_view_limit>0 && $yx_lt_friend_view>0){
                    $temp_set_show_num = $yx_lt_friend_view * $yx_lt_friend_view_limit;
                    $set_show_num = min($set_show_num,$temp_set_show_num);
                }
                if (count($friend_help_list['friend_list'])<1){
                    $max_num = $set_show_num;
                    for ($i=0;$i<$max_num;$i++){
                        $temp_arr = ['icon'=>''];
                        array_push($help_list,$temp_arr);
                    }
                }else{
                    foreach ($friend_help_list['friend_list'] as $key => $val){
                        $max = min($set_show_num,count($friend_help_list['friend_list']));
                        if ($key<$max){
                            if ($val && $val['avatar'])
                            {
                                $temp_arr = ['icon'=>$val['avatar']];
                                array_push($help_list,$temp_arr);
                            }else{
                                $temp_arr = ['icon'=>''];
                                array_push($help_list,$temp_arr);
                            }
                        }
                    }
                    $diff_num = max(0,($set_show_num-count($friend_help_list['friend_list'])));
//                    $new_diff_num = min($diff_num,(5-1));
                    for ($j=0;$j<$diff_num;$j++){
                        $temp_arr = ['icon'=>''];
                        array_push($help_list,$temp_arr);
                    }
                }



                //好友助力
                $yx_lt_friend_help = (int)$_C['yx_lt_friend_help'];
                $yx_lt_friend_help_limit = (int)$_C['yx_lt_friend_help_limit'];
                if ($yx_lt_friend_help_limit>0 && $yx_lt_friend_help>0){
                    $temp_set_help_num = $yx_lt_friend_help * $yx_lt_friend_help_limit;
                    $set_help_num = min($set_help_num,$temp_set_help_num);
                }
                if (count($help_friend_list['friend_list'])<1){
                    $max_num = $set_help_num;
                    for ($i=0;$i<$max_num;$i++){
                        $temp_arr = ['icon'=>''];
                        array_push($friend_help_arr,$temp_arr);
                    }
                }else{
                    foreach ($help_friend_list['friend_list'] as $key => $val){
                        $max = min($set_help_num,count($help_friend_list['friend_list']));
                        if ($key<$max){
                            if ($val && $val['avatar'])
                            {
                                $temp_arr = ['icon'=>$val['avatar']];
                                array_push($friend_help_arr,$temp_arr);
                            }else{
                                $temp_arr = ['icon'=>''];
                                array_push($friend_help_arr,$temp_arr);
                            }
                        }
                    }
                    $diff_num = max(0,($set_help_num-count($help_friend_list['friend_list'])));
                    for ($j=0;$j<$diff_num;$j++){
                        $temp_arr = ['icon'=>''];
                        array_push($friend_help_arr,$temp_arr);
                    }
                }


                //订单
                if (count($friend_order_list['friend_list'])<1){
                    $max_num = $set_help_num;
                    for ($i=0;$i<$max_num;$i++){
                        $temp_arr = ['icon'=>''];
                        array_push($friend_order_arr,$temp_arr);
                    }
                }else{
                    foreach ($friend_order_list['friend_list'] as $key => $val){
                        $max = min($set_help_num,count($friend_order_list['friend_list']));
                        if ($key<$max){
                            if ($val && $val['avatar'])
                            {
                                $temp_arr = ['icon'=>$val['avatar']];
                                array_push($friend_order_arr,$temp_arr);
                            }else{
                                $temp_arr = ['icon'=>''];
                                array_push($friend_order_arr,$temp_arr);
                            }
                        }
                    }
                    $diff_num = max(0,($set_help_num-count($friend_order_list['friend_list'])));
                    for ($j=0;$j<$diff_num;$j++){
                        $temp_arr = ['icon'=>''];
                        array_push($friend_order_arr,$temp_arr);
                    }
                }

                $limit_chance_tips = '';
                $used_chance = LotteryModel::getUserFlag($fans['uid'],$themeid);
                $user_total_chance = $used_chance+$remain_num;
                if ($user_total_chance>$_C['yx_lt_chance_limit']){
                    $limit_chance_tips = '每个用户最多可抽'.$_C['yx_lt_chance_limit'].'次,您已达到最高抽奖机会';
                }

                $format_res = [
                    'error_txt'=>$error_txt,
                    'top_img'=>$top_img,
                    'lt_btn_txt'=>$lt_btn_txt,
                    'remain_num'=>$remain_num,
                    'my_prize'=>$my_prize,
                    'show_tips'=>$tips,
                    'can_lt'=>$can_lt,
                    'show_no_chance_num'=>max(0,(int)$show_no_chance_num),
                    'prize_info'=>$prize_info,
                    'has_lucky_star'=>$has_lucky_star,
                    'rules'=>$rules,
                    'lucky_star_info'=>$lucky_star_info,
                    'record_list'=>$record_list,
                    'method_list'=>$method_list,
                    'lose_lottery_info'=>$lose_lottery_info,
                    'used_lt'=>$used_lt,
                    'yx_lt_receive_con'=>$yx_lt_receive_con,
                    'yx_lt_show_flag_method'=>$_C['yx_lt_show_flag_method'],
                    'yx_lt_share_lead_img'=>$_C['yx_lt_share_lead_img'],
                    'help_list'=>$help_list,
                    'friend_help_list'=>$friend_help_arr,
                    'friend_order_list'=>$friend_order_arr,
                    'limit_chance_tips'=>$limit_chance_tips,
                    'show_chance_tip'=>$show_tip,
                    'show_my_prize'=>$show_my_prize,
                    'fans_info'=>$fans,
                    'from_fans'=>$from_fans,
                    'lt_diy_prize_imgs'=>$lt_diy_prize_imgs,
                    'yx_lt_top_info_bg'=>$config['yx_lt_top_info_bg'],
                    'friend_visit'=>$friend_visit,
                    'can_help_friend'=>$can_help_friend,
                    'yx_lt_help_target'=>$yx_lt_help_target,
                ];
                //新版本抽奖
                if ($config['yx_lt_mode']==1){
                    $format_res['jubf'] = $config['jubf'];//店铺信息
                    $friend_tips = '';
                    if ($config['yx_lt_friend_help']>0 && $config['yx_lt_friend_help_limit']>0){
                        $format_res['friend_tips'] = '每邀请'.$config['yx_lt_friend_help'].'位好友可抽奖1次(最多抽'.$config['yx_lt_friend_help_limit'].'次)';
                    }
                    $friend_help_list = LotteryModel::getFriendHelpInfo($theme['id'],$_C,$fans['uid'],0,12,'friend_help')['friend_list'];
                    if ($friend_help_list && is_array($friend_help_list)){
                        $format_res['help_list'] = array_chunk($friend_help_list,6);
                    }else{
                        $format_res['help_list'] = [];
                    }

                    $format_res["other_form"] = Otherform::getBaseInfoAndInput($_S, $theme, $config, $fans);
                    //获取分享信息
                    $myworker = Worker::getThemeWorkerByOpenid($_G['openid'],$themeid,1);
                    if (empty($myworker) ){
                        if(!empty($themyrecord)){
                            $workerid = $themyrecord['workerid'];
                        }else{
                            $workerid = intval($_P['workerid']);
                        }
                        if ($workerid==0){
                            $relation = Relation::getByThemeId($themeid,$fans['openid']);
                            $workerid = $relation['workerid'];
                        }
                        $theyworker = Worker::getThemeWorkerByUid($workerid,$themeid,1);
                        $workername = $theyworker['username'];
                    }else{
                        $workername = $myworker['username'];
                    }

                    //如果付款显示当前团
                    $themyrecord = Record::getLastGroupByUid($fans['uid'], $themeid);
                    if (!empty($themyrecord)) {
                        if ($themyrecord['type'] == 1) {
                            $recordid = $themyrecord['pid'];
                        } else {
                            $recordid = $themyrecord['id'];
                        }
                    }else{
                        $recordid = 0;
                    }
                    $shareinfo = Theme::getshareinfo($_S,$theme,$config,$fans,'',$workername,$recordid,$_P['fromuid']);
                    $format_res["shareinfo"]=$shareinfo;
                    $format_res["friend_visit"]=$friend_visit;
                    $format_res["can_help_friend"]=$can_help_friend;
                    $format_res["bottombut"]=$bottombut;
                    $format_res["yx_lt_top_img"]=$config['yx_lt_top_img'];

                }

//                手机H5  页面模版
                if ($fromtype=='mobile') {
                    //页面代码数据填充
                    $ruleBtn_html = '';
                    if ($rules){
                        $ruleBtn_html = '<a href="javascript:void(0)" class="ruleBtn">活动规则</a>'.PHP_EOL;
                    }

                    //todo class=topBox
                    $topBox_html = '<div class="topBox" style="background: url('.$_C['yx_lt_top_img'].') center top no-repeat;
    padding-top: 4.55rem;
    height: 12.52rem;
    background-size: 100% auto;">'.PHP_EOL;
                    $prize_info_html = '<div class="gameBar" id="luck">'.PHP_EOL;
                    foreach ($prize_info as $key => $val) {
                        $prize_info_html .= '<span class="luck-unit luck-unit-'.$key.'" data-key="'.$key.'" data-type="'.$val['type'].'">'.PHP_EOL;
//                        if ($val['default_type']==2) {
//                            $prize_info_html .= '<i class="thank"></i>'.PHP_EOL;
//                        } else {
//                            $prize_info_html .= '<div class="giftCode">'.PHP_EOL;
//                            $prize_info_html .= '<div class="giftInfor">'.PHP_EOL;
//                            $prize_info_html .= '<div class="giftImg">'.PHP_EOL;
//                            $prize_info_html .= '<img src="'.$val['icon'].'">'.PHP_EOL;
//                            $prize_info_html .= '<p>'.$val['title'].'</p>'.PHP_EOL;
//                            $prize_info_html .= '</div>'.PHP_EOL;
//                            $prize_info_html .= '</div>'.PHP_EOL;
//                            $prize_info_html .= '</div>'.PHP_EOL;
//                        }
                        $prize_info_html .= '<div class="giftCode">'.PHP_EOL;
                        $prize_info_html .= '<div class="giftInfor">'.PHP_EOL;
                        $prize_info_html .= '<div class="giftImg">'.PHP_EOL;
                        $prize_info_html .= '<img src="'.$val['icon'].'">'.PHP_EOL;
                        $prize_info_html .= '<p>'.$val['title'].'</p>'.PHP_EOL;
                        $prize_info_html .= '</div>'.PHP_EOL;
                        $prize_info_html .= '</div>'.PHP_EOL;
                        $prize_info_html .= '</div>'.PHP_EOL;
                        $prize_info_html.='</span>'.PHP_EOL;
                    }
                    $all_qty = LotteryModel::getAllPrizeQty($themeid);
                    if ($all_qty>0){
                        $now = time();
                        if ($now<$_C['yx_lt_begintime']){
                            $can_lt = false;
                            $prize_info_html.='<span id="btn22"><div style="color: #ffffff;font-size: .3rem;font-weight: bold;margin: 22px auto;margin-bottom:0;text-align: center;">抽奖未开始</div><div id="notice_chance" style="color: #ffffff;font-size: 12px;margin: 6px auto;text-align: center;">抽奖未开始</div></span>'.PHP_EOL;
                        }else if ($now>$_C['yx_lt_endtime']){
                            $can_lt = false;
                            $prize_info_html.='<span id="btn22"><div style="color: #ffffff;font-size: .3rem;font-weight: bold;margin: 22px auto;margin-bottom:0;text-align: center;">抽奖结束</div><div id="notice_chance" style="color: #ffffff;font-size: 12px;margin: 6px auto;text-align: center;">抽奖活动结束</div></span>'.PHP_EOL;
                        }elseif ($remain_num>0) {
                            $prize_info_html .= '<span id="btn22"><div style="color: #ffffff;font-size:.3rem;font-weight: bold;margin: 22px auto;margin-bottom:0;text-align: center;">开始抽奖</div><div id="notice_chance" style="color: #ffffff;font-size: 12px;margin: 6px auto;text-align: center;">还剩' . $remain_num . '次</div></span>' . PHP_EOL;
                        }else{
                            $prize_info_html.='<span id="btn22"><div style="color: #ffffff;font-size: .3rem;margin: 22px auto;margin-bottom:0;text-align: center;">获取更多机会</div><div id="notice_chance" style="color: #ffffff;font-size: 12px;margin: 6px auto;text-align: center;">还剩' . $remain_num . '次</div></span>'.PHP_EOL;
                        }

                    }else{
                        $prize_info_html.='<span id="btn22"><div style="color: #ffffff;font-size: .3rem;font-weight: bold;margin: 22px auto;margin-bottom:0;text-align: center;">抽奖结束</div><div id="notice_chance" style="color: #ffffff;font-size: 12px;margin: 6px auto;text-align: center;">抽奖活动结束</div></span>'.PHP_EOL;
                    }



                    $prize_info_html.='</div>'.PHP_EOL;

                    if ($_C['yx_lt_friend_view']>0){
                        $gameControl_html = '<div class="gameControl">'.PHP_EOL.'<a class="moreChance">获取机会</a>'.PHP_EOL.'<a class="myGet" href="javascript:void(0)">我的奖品</a>'.PHP_EOL.'</div>';
                    }else{
                        $gameControl_html = '<div class="gameControl">'.PHP_EOL.'<a class="moreChance">获取机会</a>'.PHP_EOL.'<a class="myGet" href="javascript:void(0)">我的奖品</a>'.PHP_EOL.'</div>';

//                    $gameControl_html = '<div class="gameControl">'.PHP_EOL.'<a class="myGet" href="javascript:void(0)">我的奖品</a>'.PHP_EOL.
//                        '<a class="chance_num" id="chance_num">抽奖机会：'.$remain_num.'次</a>'.PHP_EOL.'</div>';
                    }
                    $topBox_html .= $prize_info_html.$gameControl_html.'</div>';

                    //todo 中奖记录及攻略
                    $winData_html = '<div class="winData">'.PHP_EOL.'<div class="dataBox">'.PHP_EOL.'<div class="dataChose">'.PHP_EOL.
                        '<span class="cur">中奖记录</span>'.PHP_EOL.'<span>抽奖攻略</span>'.PHP_EOL.'</div>'.PHP_EOL.'<div class="yx_choseBox">'.PHP_EOL;
                    //-- 中奖记录
                    $peopleList = '<div class="yx_choseBar yx_peopleList">'.PHP_EOL.'<ul>'.PHP_EOL;
                    if ($record_list){
                        foreach ($record_list as $key => $val) {
                            $peopleList .= '<li>'.PHP_EOL.'<div class="peoImg">'.PHP_EOL.'<img src="';
                            $peopleList .= $val['avatar'].'">'.PHP_EOL.'</div>'.PHP_EOL.'<div class="peoName">'.PHP_EOL;
                            $peopleList .= $val['username'].'</div>'.PHP_EOL.'<div class="peoInfor">'.PHP_EOL;
                            $peopleList .= $val['tips'].'</div>'.PHP_EOL.'<div class="peoTime">'.PHP_EOL;
                            $peopleList .= '<p>'.substr($val['created_at'],0,10).'</p>'.PHP_EOL.'<p>'.substr($val['created_at'],10,9).'</p>'.PHP_EOL.' </div>'.PHP_EOL.'</li>';
                        }
                    }
                    $peopleList.= '</ul>'.PHP_EOL.'</div>'.PHP_EOL;
                    //-- 抽奖攻略
                    $strategy = '<div class="yx_choseBar strategy">'.PHP_EOL.'<ul>'.PHP_EOL;
                    if ($method_list) {
                        foreach ($method_list as $key => $val) {
                            if ($val['type']!='timeset') {
                                if ($val['type']=='order') {
                                    $strategy.= '<li>'.PHP_EOL.'<img src="'.$val['icon'].'">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL;
                                    $strategy.= '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.'
                                    <p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL;
                                    $strategy.= '<a href="javascript:;" onclick="show_register_info()" data-status="'.$val['status'].'" data-type="'.$val['type'].'" data-timeset="'.$val['timeset'].'"
                               data-otherid="'.$val['other_id'].'" class="stratTag">'.$val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                                } elseif ($val['type']=='share' || $val['type']=='share_moments') {
                                    $strategy.= '<li>'.PHP_EOL.'<img src="'.$val['icon'].'">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL;
                                    $strategy.= '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.'
                                    <p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL;
                                    $strategy.= '<a href="javascript:;" onclick="show_share()" data-status="'.$val['status'].'" data-type="'.$val['type'].'" data-timeset="'.$val['timeset'].'"
                               data-otherid="'.$val['other_id'].'" class="stratTag">'.$val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                                } elseif ($val['type']=='helpFriend' || $val['type']=='friendHelp'){
                                    //计算总数
                                    $set_show_num = 6;
                                    if ($val['type']=='helpFriend'){
                                        //好友到访
                                        $yx_lt_friend_view = (int)$_C['yx_lt_friend_view'];
                                        $yx_lt_friend_view_limit = (int)$_C['yx_lt_friend_view_limit'];
                                        if ($yx_lt_friend_view_limit>0 && $yx_lt_friend_view>0){
                                            $temp_set_show_num = $yx_lt_friend_view * $yx_lt_friend_view_limit;
                                            $set_show_num = min($set_show_num,$temp_set_show_num);
                                        }
                                    }else{
                                        //好友助力
                                        $yx_lt_friend_view = (int)$_C['yx_lt_friend_help'];
                                        $yx_lt_friend_view_limit = (int)$_C['yx_lt_friend_help_limit'];
                                        if ($yx_lt_friend_view_limit>0 && $yx_lt_friend_view>0){
                                            $temp_set_show_num = $yx_lt_friend_view * $yx_lt_friend_view_limit;
                                            $set_show_num = min($set_show_num,$temp_set_show_num);
                                        }
                                    }


                                    $strategy.= '<li class="li_f">'.PHP_EOL.'<img src="'. $val['icon']. '">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL.
                                        '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.
                                        '<p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL.
                                        '<a href="javascript::" onclick="show_share()" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'" class="stratTag">'.
                                        $val['btn_txt'].'</a>'.PHP_EOL.'</li><li class="f_list li_np" onclick="helpFriend(this)" data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'">'.

                                        $strategy_tmp = '<div>';
                                    $friend_help_list = LotteryModel::getFriendHelpInfo($themeid,$_C,$fans['uid']);
                                    if (count($friend_help_list['friend_list'])<1){
                                        for ($i=1;$i<=$set_show_num;$i++){
                                            $strategy_tmp .=  '<div class="f_list_item"><div class="f_list_img_wrap"><span>+</span><img/></div></div>';
                                        }

                                    }else{
                                        foreach ($friend_help_list['friend_list'] as $key => $val){
                                            if ($key<=$set_show_num){
                                                if ($val && $val['avatar'])
                                                {
                                                    $strategy_tmp .= '<div class="f_list_item"><div class="f_list_img_wrap"><span>+</span><img src="'.$val['avatar'].'"/></div></div>';
                                                }else{
                                                    $strategy_tmp .=  '<div class="f_list_item"><div class="f_list_img_wrap"><span>+</span><img/></div></div>';
                                                }
                                            }
                                        }
                                        $diff_num = $set_show_num-count($friend_help_list['friend_list']);
                                        if ($diff_num>0){
                                            for ($j=0;$j<$diff_num;$j++){
                                                $strategy_tmp .=  '<div class="f_list_item"><div class="f_list_img_wrap"><span>+</span><img/></div></div>';
                                            }
                                        }
                                    }

                                    $strategy_tmp.= '</div><a href="javascript::"  data-status="'.$val['status'].'" data-type="'. $val['type']. '" data-timeset="'. $val['timeset']. '" data-otherid="'.$val['other_id'].'">查看全部</a></li>'.PHP_EOL;
                                    $strategy .= $strategy_tmp;
                                }else {
                                    $strategy.= '<li>'.PHP_EOL.'<img src="'.$val['icon'].'">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL;
                                    $strategy.= '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.'
                                    <p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL;
                                    $strategy.= '<a  data-status="'.$val['status'].'" data-type="'.$val['type'].'" data-timeset="'.$val['timeset'].'"
                               data-otherid="'.$val['other_id'].'" class="stratTag">'.$val['btn_txt'].'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                                }

                            } else {

                                $strategy.= '<li>'.PHP_EOL.'<img src="'.$val['icon'].'">'.PHP_EOL.'<div class="statTxt">'.PHP_EOL;
                                $strategy.= '<h2>'.$val['title'].PHP_EOL.'<span>'.$val['num'].'</span>'.PHP_EOL.'</h2>'.PHP_EOL.'
                                <p>'.$val['tips'].'</p>'.PHP_EOL.'</div>'.PHP_EOL;
                                //判断时间戳
                                $temp_btn_txt = $val['btn_txt']?$val['btn_txt']:'后可领';
                                $strategy.= '<a  data-status="'.$val['status'].'" data-type="'.$val['type'].'" data-timeset="'.$val['timeset'].'"
                               data-otherid="'.$val['other_id'].'" class="stratTag timeEnd12">'.PHP_EOL.'<div class="yx_yomibox mobile_color" data="'.$val['timeset'].'"></div>'.PHP_EOL.
                                    $temp_btn_txt.'</a>'.PHP_EOL.'</li>'.PHP_EOL;
                            }
                        }
                    }
                    $strategy .='</ul>'.PHP_EOL.'</div>';
                    $winData_html.=$peopleList.$strategy.'</div>'.PHP_EOL.'</div>'.PHP_EOL.'</div>'.PHP_EOL;
                    if ($_C['yx_lt_show_flag_method']==1){
                        $winData_html = '';
                    }

                    //TODO 次数用完
                    $noTimesAlt_html = '';
                    if (count($my_prize['prize_info'])>0){
                        $noTimesAlt_html = '<div class="noTimesAlt">'.PHP_EOL.'<div class="notimeBar">'.PHP_EOL.'<h2>'.$lose_lottery_info['title'].'</h2>'.PHP_EOL.
                            '<p>'.$lose_lottery_info['desc'].'</p>'.PHP_EOL.'<div class="proList">'.PHP_EOL.'<h3>'.$lose_lottery_info['more_prize_title'].'</h3>'.PHP_EOL.'<ul>'.PHP_EOL;
                        if ($lose_lottery_info['more_prize_list']) {
                            foreach ($lose_lottery_info['more_prize_list'] as $t => $item) {
                                $noTimesAlt_html.= '<li>'.PHP_EOL.'<div class="proImg">'.PHP_EOL.'<img mode="widthFix" src="'. $item['img']. '">'.PHP_EOL.
                                    '</div>'.PHP_EOL.'<span>'. $item['name']. '</span>'.PHP_EOL.'</li>'.PHP_EOL;
                            }
                        }
                        $noTimesAlt_html .= '</ul>'.PHP_EOL.'</div>'.PHP_EOL.'<div class="controlBox">'.PHP_EOL.'<a href="#luck" class="close">继续抽奖</a>'.PHP_EOL.'</div>'.PHP_EOL.'</div>'.PHP_EOL.
                            '<span class="close"></span>'.PHP_EOL.'</div>'.PHP_EOL;
                    }



                    //TODO 规则弹窗
                    $ruleBar_html = '<div class="yx_altBox" id="ruleBar">'.PHP_EOL.'<div class="ruleBox">'.PHP_EOL.'<div class="ruleBar">'.PHP_EOL.'<div class="ruleTit">活动规则</div>'.PHP_EOL;
                    if ($rules) {
                        foreach ($rules as $t => $item) {
                            $ruleBar_html .= '<p>'.$item['rule_sort'].'-'.$item['rule_content'].'</p>'.PHP_EOL;
                        }
                    }
                    $ruleBar_html .= '</div>'.PHP_EOL.'</div>'.PHP_EOL.'<span class="close"></span>'.PHP_EOL.'</div>'.PHP_EOL;

                    //TODO  幸运星
                    $luckyStar_html = '';
                    if ($has_lucky_star) {
                        $luckyStar_html.= '<div class="yx_altBox" id="luckyStar">'.PHP_EOL.'<div class="luckyAlt">'.PHP_EOL.'<h2>'.$lucky_star_info['title'].'</h2>'.PHP_EOL.
                            '<div class="luckyImg">'.PHP_EOL.'<img src="'.$lucky_star_info['icon'].'">'.PHP_EOL.'<i></i>'.PHP_EOL.'</div>'.PHP_EOL.
                            '<p>'.$lucky_star_info['content'].'</p>'.PHP_EOL.'<span>'.$lucky_star_info['tips'].'</span>'.PHP_EOL.'<a class="goPlay">'.$lucky_star_info['btn_txt'].'</a>'.
                            PHP_EOL.'</div>'.PHP_EOL.'<span class="close"></span>'.PHP_EOL.'</div>'.PHP_EOL;
                    }

//                    $screen_html = '<div id="screen" class="screen"></div>';
                    $screen_html = '';
                    $share_html = '<div id="show_share" style="width:300px;height: 240px;position: fixed;top: 1%;display: none;right: 1%;z-index: 5;">'.PHP_EOL.
                        '<img mode="widthFix" src="'.$_C['yx_lt_share_lead_img'].'">'.PHP_EOL.'</div>';

                    //兑换信息
                    $my_convert_info = [
                        'prize_info'=>$my_prize['prize_info'],
                        'convert_title_prefix'=>'中奖啦，恭喜您抽中了',
                        'yx_lt_convert_method'=>"【兑奖方式】".$_C['yx_lt_convert_method'],
                        'yx_lt_convert_tel'=>"电话：".$_C['yx_lt_convert_tel'],
                        'yx_send_people'=>"  发奖人：".$_C['yx_send_people'],
                        'yx_send_mobile'=>"联系方式：".$theme['mobile'],
                        'yx_btn_txt'=>$_C['yx_lt_convert_btn'],
                        'yx_btm_tips'=>$_C['yx_lt_convert_btm_tips'],
                    ];
                    //兑换码
                    $convert_html = '<div class="winerAlt">'.PHP_EOL.'<span class="closeWiner"></span>'.PHP_EOL.'<div class="winBox">'.PHP_EOL.'<h4></h4>'.PHP_EOL.
                        '<div class="top_div">'.
                        '<div class="winImg">'.PHP_EOL.'<img class="convert_prize_img" src="">'.PHP_EOL.'</div>'.PHP_EOL.'<div class="winTxt">'.PHP_EOL.
                        '<p class="convert_title">中奖啦，恭喜你抽中了</p>'.PHP_EOL.'<p class="convert_prize_name"></p>'.PHP_EOL.'</div>'.PHP_EOL.'</div>'.PHP_EOL.
                        '<div class="ticketBox">'.PHP_EOL.'<p>兑奖凭证</p>'.PHP_EOL.'<span class="convert_code"><img src="" id="verif_img" style="width: 130px;height: 136px;"></span>'.PHP_EOL.'</div>'.PHP_EOL.
                        '<div class="changeNotice">'.PHP_EOL.'<p class="convert_method"></p>'.PHP_EOL.
                        '<p class="convert_people"></p>'.PHP_EOL.'<p class="convert_tel"></p>'.PHP_EOL.'</div>'.PHP_EOL.
                        '<a  id="convert_btn_txt" class="changeBtn">'.$_C['yx_lt_convert_btn'].'</a>'.PHP_EOL.
                        '<div class="changepoint" id="convert_btm_tips">'.$_C['yx_lt_convert_btm_tips'].'</div>'.PHP_EOL.
                        '</div>'.PHP_EOL.'</div>'.PHP_EOL;

                    //我的奖品
                    $mygiftBar_html = '<div class="yx_altBox" id="mygiftBar">'.PHP_EOL.
                        '<div class="myGift">'.PHP_EOL.'<div style="color: #d33201;">奖品介绍</div>'.PHP_EOL.'<div class="gifPicture" style="padding-top: 0;">'.PHP_EOL.'<img style="width: 4.28rem;height: 4.28rem;" src="">'.PHP_EOL.
                        '</div>'.PHP_EOL.'<h2 class="myGift_h2">'.PHP_EOL.'<span></span>'.PHP_EOL.'</h2>'.PHP_EOL.'<p class="myGift_p"></p>'.PHP_EOL.
                        '<a  class="backPlay"></a>'.PHP_EOL.'</div>'.PHP_EOL.'<span class="close"></span>'.PHP_EOL.'</div>';

                    if($_S['autored']==0){
                        $red_button= array('button'=>"查看中奖记录",'tips'=>"红包将在24小时内到您的微信零钱");
                    }elseif($_S['autored']==1){
                        $red_button= array('button'=>"查看中奖记录",'tips'=>"红包已发放至您的微信零钱，请注意查收");
                    }elseif($_S['autored']==2){
                        $red_button= array('button'=>"去提现",'tips'=>"红包达到".$_C['red_package_min_amount']."元即可提现");
                    }
                    $red_package_modal_html = '  <div class="yx_altBox" id="redWrap" style="display: none">
                            <div class="redWrap" style="padding-top:0;height:50vh;width:70vw">
                              <div class="h">
                                <div><text class="l"></text>现金红包最高可获得<text class="r"></text></div>
                              </div>
                              <div class="price">
                                <span class="num"><span class="p"></span><span class="flag">元</span></span>
                              </div>
                              <div class="btn backPlay">快去抽奖</div>
                            </div>
                            <span class="close"></span>
                          </div>
                          <div class="yx_altBox" id="redGetWrap" style="display: none">
                            <div class="redGetWrap" style="padding-top:0;height:60vh;width:70vw">
                              <div class="h">恭喜你获得</div>
                              <div class="name">现金红包</div>
                              <div class="price">
                                <text class="num"><span class="p"></span><text class="flag">元</text></text>
                              </div>
                              <div class="btn convert_btn_txt">'.$red_button["button"].'</div>
                              <div class="anim_txt">'.$red_button["tips"].'</div>
                            </div>
                            <span class="close"></span>
                          </div>';

                    //格式化返回数据
                    $mobile_res = [
                        'ruleBtn_html'=>Theme::encode($ruleBtn_html),
                        'topBox_html'=>Theme::encode($topBox_html),
                        'winData_html'=>Theme::encode($winData_html),
                        'noTimesAlt_html'=>Theme::encode($noTimesAlt_html),
                        'ruleBar_html'=>Theme::encode($ruleBar_html),
                        'luckyStar_html'=>Theme::encode($luckyStar_html),
                        'screen_html'=>Theme::encode($screen_html),
                        'share_html'=>Theme::encode($share_html),
                        'mygiftBar_html'=>Theme::encode($mygiftBar_html),
                        'convert_html'=>Theme::encode($convert_html),
                        'red_package_modal_html'=>Theme::encode($red_package_modal_html),
                    ];
                    $format_res = array_merge($format_res,$mobile_res);
                }
                return_json(0,'操作成功',$format_res);
            }
        }
        return_json(0,'操作成功',$format_res);
    }

    function getTimeset()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if (!$_G['ispost']) {
            return_json(1,'非法请求');
        } else {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            if (!$themeid || !$theme) {
                return_json(1,'活动入口不正确');
            } else {
                $t_id = $_P['t_id'];
                $fans = $this->checkuser();
                if ($fans && $fans['uid'] && $t_id){
                    $where = ['t_id'=>$t_id,'uid'=>$fans['uid'],'themeid'=>$themeid];
                    $get_item = LotteryModel::getTimeSetFlagItem($where);
                    if ($get_item && $get_item['status']==1) {
                        $where = ['t_id'=>$get_item['t_id']];
                        $data = ['status'=>3];
                        LotteryModel::editTimeSetItem($where,$data);
                        $res =  LotteryModel::apiAddChance('timeset',$fans,$themeid,$fans['uid'],$_C,$get_item['t_id']);

                        $format_res = ['error_txt'=>''];
                        if ($fromtype=='wxapp'){
                            //判断用户是否存在订单
                            $themyrecord = Record::getLastGroupByUid($fans['uid'],$themeid);
                            $res = LotteryModel::indexMethod($theme,$_C,$fans['uid'],$fromuid,$themyrecord);

                            $error_txt = isset($res['error_txt'])?$res['error_txt']:'';
                            $top_img = isset($res['top_img'])?$res['top_img']:'';
                            $lt_btn_txt = isset($res['lt_btn_txt'])?$res['lt_btn_txt']:'抽奖';
                            $remain_num = isset($res['remain_num'])?$res['remain_num']:0;
                            $tips = isset($res['tips'])?$res['tips']:'';
                            $can_lt = isset($res['can_lt'])?$res['can_lt']:false;
                            $show_modal_num = isset($res['show_modal_num'])?$res['show_modal_num']:9999;

                            $prize_info = isset($res['prize_info'])?$res['prize_info']:[];
                            $record_list = isset($res['record_list'])?$res['record_list']:[];
                            $method_list = isset($res['method_list'])?$res['method_list']:[];
                            $lose_lottery_info = isset($res['lose_lottery_info'])?$res['lose_lottery_info']:[];
                            $rules = isset($res['rules'])?$res['rules']:[];
                            $has_lucky_star = isset($res['has_lucky_star'])?$res['has_lucky_star']:false;
                            $lucky_star_info = isset($res['lucky_star_info'])?$res['lucky_star_info']:[];

                            $format_res = [
                                'error_txt'=>$error_txt,
                                'top_img'=>$top_img,
                                'lt_btn_txt'=>$lt_btn_txt,
                                'remain_num'=>$remain_num,
                                'show_tips'=>$tips,
                                'can_lt'=>$can_lt,
                                'show_modal_num'=>$show_modal_num,
                                'prize_info'=>$prize_info,
                                'has_lucky_star'=>$has_lucky_star,
                                'rules'=>$rules,
                                'lucky_star_info'=>$lucky_star_info,
                                'record_list'=>$record_list,
                                'method_list'=>$method_list,
                                'lose_lottery_info'=>$lose_lottery_info
                            ];
                        }
                        if ($res){
                            return_json(1,$res,$format_res);
                        }else{
                            return_json(0,'操作成功',$format_res);
                        }

                    }
                }else{
                    $format_res = [
                        'error_txt'=>'发生了意外',
                        'top_img'=>'',
                        'lt_btn_txt'=>'',
                        'remain_num'=>0,
                        'show_tips'=>'',
                        'can_lt'=>false,
                        'show_modal_num'=>0,
                        'prize_info'=>[],
                        'has_lucky_star'=>false,
                        'rules'=>[],
                        'lucky_star_info'=>[],
                        'record_list'=>[],
                        'method_list'=>[],
                        'lose_lottery_info'=>[]
                    ];
                    return_json(1,'操作失败',$format_res);
                }
                return_json(1,'操作失败');
            }
        }
    }

    function subUserInfo()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        if (!$_G['ispost']) {
            return_json(1,'非法请求');
        } else {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            if (!$themeid || !$theme) {
                return_json(1,'活动入口不正确');
            } else {
                $receive_name = $_P['receive_name'];
                $receive_phone = $_P['receive_phone'];
                $fans = $this->checkuser();
                if ($fans && $fans['uid'] && $receive_name && $receive_phone){
                    //用户日志收集
                    Userdata::addLog($themeid, $fans['uid'], 17, '抽奖客资收集');
                    $result = LotteryModel::updateFansInfo($fans['uid'],$themeid,['username'=>$receive_name,'mobile'=>$receive_phone]);
                    $format_res = [
                        'error_txt'=>'',
                        'result'=>true
                    ];
                    return_json(0,'操作成功',$format_res);
                }
                return_json(1,'操作失败');
            }
        }
    }

    function friendHelp()
    {
        global $_P,$_G,$_S,$_C;
        $fromtype = $_P['from'];
        $themeid = isset($_P['themeid'])?$_P['themeid']:'';
        $theme = Theme::getThemeById($themeid);
        $workerid = isset($_P['workerid'])?$_P['workerid']:'';
        $type = isset($_P['type'])?$_P['type']:'helpFriend';

        $page_index = max(1,intval($_P['page']));
        $page_size = 12;
        $page_start = ($page_index-1)*$page_size;
        $new_type ='friend_view';

        if ($type=='friendHelp'){
            $new_type = 'friend_help';
        }else if ($type=='friendOrder'){
            $new_type = 'friend_order';
        }
        if (!$themeid || !$theme) {
            return_json(1,'活动入口不正确');
        } else {
            if ($fromtype=='mobile') {
                $fans = $this->checkuser();
                if ($fans && $fans['uid']){
                    $uid = $fans['uid'];

                    $friend_help_list = LotteryModel::getFriendHelpInfo($theme['id'],$_C,$uid,$page_start,$page_size,$new_type);
                    $friend_help_total = LotteryModel::getAllFriendHelpLTotal($themeid,$uid);
                    $max_page = ceil($friend_help_total/$page_size);
                    //TODO 好友助力
                    $helpBar_html = '<div class="yx_altBox" id="helpBar">'.PHP_EOL.'<div class="friendHelp">'.PHP_EOL.'<h2>'. $friend_help_list['title'].'</h2>'.PHP_EOL.'<h3>'.
                        $friend_help_list['top_tips'] .'</h3>'.PHP_EOL.'<ul>';
                    if ($friend_help_list['friend_list']) {
                        foreach ($friend_help_list['friend_list'] as $t => $item) {
                            $helpBar_html.='<li>'.PHP_EOL.'<img src="'.$item['avatar'].'">'.PHP_EOL.'<p>'.$item['nickname'].'</p>'.PHP_EOL.'</li>'.PHP_EOL;
                        }
                    }
                    $helpBar_html.= '</ul>'.PHP_EOL.'<a  class="goPlay">'.$friend_help_list['btn_txt'].'</a>'.PHP_EOL.'</div>'.PHP_EOL.'<span class="close"></span>'.PHP_EOL.'</div>'.PHP_EOL;

                    $format_res = [
                        'helpBar_html'=>Theme::encode($helpBar_html),
                        'max_page'=>$max_page,
                    ];
                    return_json(0,'操作成功',$format_res);
                }
                return_json(1,'操作失败',[]);
            } elseif ($fromtype=='wxapp') {
                $fans = $this->checkuser();
                if ($fans && $fans['uid']){
                    $uid = $fans['uid'];
                    $friend_help_list = LotteryModel::getFriendHelpInfo($theme['id'],$_C,$uid,$page_start,$page_size,$new_type);
                    $format_res = [
                        'friend_help_list'=>$friend_help_list,
                    ];
                    return_json(0,'操作成功',$format_res);
                }
                return_json(1,'操作失败',[]);
            }
        }
    }
    //提现
    function withdraw()
    {
        global $_P,$_G,$_S,$_C;
        if (!$_G['ispost']) {
            return_json(1,'非法请求');
        } else {
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $theme = Theme::getThemeById($themeid);
            if (!$themeid || !$theme) {
                return_json(1,'活动入口不正确');
            } else {
                $fans = $this->checkuser();
                if ($fans && $fans['uid']){
                    $fromuid = $_P['fromuid']?$_P['fromuid']:$fans['uid'];
                    //手动提现
                    $res = LotteryModel::Withdraw($themeid,$_C,$fans['uid'],$fromuid);
                    if ($res){
                        return_json(0,'操作成功',$res);
                    }else{
                        return_json(1,'提现失败',$res);
                    }

                }
            }
            return_json(1,'操作失败',$res);
        }
    }

    //助力
    function power()
    {
        global $_P,$_G,$_S,$_C;
        $config = $_C;
        if ($_G['ispost']) {
            $fromuid = isset($_P['fromuid'])?$_P['fromuid']:'';
            $fans = $this->checkuser();
            $themeid = isset($_P['themeid'])?$_P['themeid']:'';
            $from_fans = LotteryModel::getUserInfoById($fromuid,$themeid);
            //判断是否给上级增过机会记录
            $has_friend_flag = LotteryModel::getFriendViewChanceFlag($themeid,$from_fans['uid'],$fans['uid'],'friend_help');
            $themyrecord = Record::getLastGroupByUid($fromuid,$themeid);
            if ((int)$config['yx_lt_friend_help']==0){
                return_json(1,'配置错误：后台未设置好友助力',[]);
            }
            if ($from_fans && !$has_friend_flag){
                //判断上一级是否支付
                if ($config['yx_lt_help_user_type'] && $themyrecord){
                    $res = LotteryModel::apiAddChance('friend_help',$from_fans,$themeid,$fans['uid'],$config);
                }
                if(!$config['yx_lt_help_user_type']){
                    $res = LotteryModel::apiAddChance('friend_help',$from_fans,$themeid,$fans['uid'],$config);
                }
                return_json(0,'助力成功',[]);
            }
            if ($has_friend_flag){
                return_json(0,'您已助力过了',[]);
            }
            return_json(0,'您无法助力',[]);
        }else{
            return_json(0,'非法请求',[]);
        }
    }
}